package com.framework.bdf4j.sysadm.controller;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.framework.bdf4j.annotations.SysLog;
import com.framework.bdf4j.comm.util.R;
import com.framework.bdf4j.comm.web.WebUtil;
import com.framework.bdf4j.sysadm.entity.SysWelcome;
import com.framework.bdf4j.sysadm.service.SysWelcomeService;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 快速导航Controller
 * 
 * @author cfss
 * @date 2020-06-12
 */
@Slf4j
@Controller
@AllArgsConstructor
@RequestMapping("/admin/welcome")
public class SysWelcomeController {

    private final SysWelcomeService sysWelcomeService;

    
    /**
     * 新增快速导航
     *
     * @param sysWelcome 快速导航信息
     *            
     * @return success、false
     */
    @SysLog("新增快速导航")
    @PostMapping
    @ResponseBody
    public R save(SysWelcome sysWelcome) {
    	Integer userId = WebUtil.getHttpCurrentUserId();
    	sysWelcome.setUserId(userId);
    	
    	SysWelcome wel=sysWelcomeService.getOne(Wrappers.<SysWelcome>query().lambda()
				.eq(SysWelcome::getUserId, userId).eq(SysWelcome::getLabel, sysWelcome.getLabel()));
	if(wel!=null)
		return R.failed("菜单已经添加，不能重复添加！");
    	List<SysWelcome> wels = sysWelcomeService.list(Wrappers.<SysWelcome>query().lambda()
				.eq(SysWelcome::getUserId, userId));
		int sort=0;
		if(!CollectionUtil.isEmpty(wels))
			sort=wels.stream().mapToInt(SysWelcome::getSort).max().getAsInt();
    	sysWelcome.setSort(sort+1);
        return R.ok(sysWelcomeService.save(sysWelcome));
    }
    @PostMapping("/changePoint")
    @ResponseBody
    public R<Boolean> updateWelcome(String params) {
    	if(StrUtil.isBlank(params))
    		return R.failed("参数为空");
    	 Map map=JSONObject.parseObject(params);
        return R.ok(sysWelcomeService.updateWelcome(map));
    }

    /**
     * 删除快速导航
     *
     * @param ids
     * @return success/false
     */
    @SysLog("删除快速导航")
    @PostMapping("/{id}")
    @ResponseBody
    public R removeById(@PathVariable String id) {
        return R.ok(sysWelcomeService.removeById(Integer.parseInt(id)));
    }
}
